﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace SPMS.Common
{
    /// <summary>
    /// 用于处理<see cref="JsonResultEx"/>的扩展方法。
    /// </summary>
    public static class JsonResultExExtensions
    {
        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller)
        {
            //转调
            return JsonEx(controller, true, null, 0, null, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess)
        {
            //转调
            return JsonEx(controller, isSuccess, null, 0, null, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="data">返回数据。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess, object data)
        {
            //转调
            return JsonEx(controller, isSuccess, null, 0, data, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="data">返回数据。</param>
        /// <param name="total">数据总数。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess, object data, int total)
        {
            //转调
            return JsonEx(controller, isSuccess, null, 0, data, total, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="message">返回消息。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess, string message)
        {
            //转调
            return JsonEx(controller, isSuccess, message, 0, null, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="message">返回消息。</param>
        /// <param name="code">错误编码。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess, string message, int code)
        {
            //转调
            return JsonEx(controller, isSuccess, message, code, null, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="message">返回消息。</param>
        /// <param name="code">错误编码。</param>
        /// <param name="data">返回数据。</param>
        /// <param name="total">数据总数。</param>
        /// <param name="behavior">处理请求行为。</param>
        /// <returns>用于返回的<see cref="JsonResultEx"/>对象。</returns>
        public static JsonResultEx JsonEx(this Controller controller, bool isSuccess, string message, int code, object data, int total, JsonRequestBehavior behavior)
        {
            //创建结果
            JsonResultEx result = new JsonResultEx();

            //设置属性
            result.Data = new JsonResultViewModel()
            {
                code = code,
                data = data,
                message = message,
                success = isSuccess,
                total = total
            };

            //设置行为
            result.JsonRequestBehavior = behavior;
            result.ContentEncoding = Encoding.UTF8;
            result.ContentType = SPMS.Common.Properties.Resources.text_content_type_json;

            //返回结果
            return result;
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="data">返回数据。</param>
        /// <param name="total">数据总数。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx TreeData(this Controller controller, bool isSuccess, object data, int total)
        {
            //转调
            return TreeData(controller, isSuccess, null, 0, data, total, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="message">返回消息。</param>
        /// <param name="code">错误编码。</param>
        /// <returns>
        /// 用于返回的<see cref="JsonResultEx" />对象。
        /// </returns>
        public static JsonResultEx TreeData(this Controller controller, bool isSuccess, string message, int code)
        {
            //转调
            return TreeData(controller, isSuccess, message, code, null, 0, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用于返回<see cref="JsonResultEx" />的控制器扩展方法。
        /// </summary>
        /// <param name="controller">控制器。</param>
        /// <param name="isSuccess">是否执行成功。</param>
        /// <param name="message">返回消息。</param>
        /// <param name="code">错误编码。</param>
        /// <param name="data">返回数据。</param>
        /// <param name="total">数据总数。</param>
        /// <param name="behavior">处理请求行为。</param>
        /// <returns>用于返回的<see cref="JsonResultEx"/>对象。</returns>
        public static JsonResultEx TreeData(this Controller controller, bool isSuccess, string message, int code, object data, int total, JsonRequestBehavior behavior)
        {
            //创建结果
            JsonResultEx result = new JsonResultEx();

            //设置属性
            result.Data = new JsonResultTreeData()
            {
                code = code,
                children = data,
                message = message,
                success = isSuccess,
                total = total
            };

            //设置行为
            result.JsonRequestBehavior = behavior;
            result.ContentEncoding = Encoding.UTF8;
            result.ContentType = SPMS.Common.Properties.Resources.text_content_type_json;

            //返回结果
            return result;
        }
    }
}
